import React from 'react';
import { getCodeByKey } from 'src/utils/utils';
import services from 'src/services/api';
import storage from 'src/utils/storage';
import Modal from 'src/components/Modal';

export default function singleLogin(props) {
  const search = window.location.search;
  const tokenid = getCodeByKey(search, 'tokenid');
  if (tokenid) {
    singleLogin(tokenid);
  }

  function singleLogin(tokenid) {
    services.user.singleLogin({ tokenid }).then((res) => {
      if (res.code === 0) {
        storage.token = res.data.uuid;
        storage.userInfo = res.data;
        storage.dept = res.data.current_dept;
        props.history.push(res.data.default_url || '/');
      } else {
        Modal.open({
          title: '',
          isConfirm: true,
          width: '300px',
          content: <p>{res.msg}</p>,
          isOkBtn: true,
          isCancelBtn: true,
          ok: () => {
            Modal.close();
          },
          cancel: () => {
            Modal.close();
          },
        });
      }
    });
  }
  return <div />;
}
